/**
 * 用户信息添加编辑窗体
 */
user.win = user.win || Ext.extend(Ext.Window,{
	initComponent:function(){
		Ext.apply(this, {
			resizable : false,																					
			animateTarget : document.body,																		
			closeAction: 'hide',																				
			plain: true,
			modal: true,
			width: 400,
			items:this.getForm()
		}),
		user.win.superclass.initComponent.call(this);
	},
	getForm : function(){
		return {
        	xtype:'form',
            bodyStyle:'padding:10px',
            labelWidth : 80,
            defaults : {
            	width : 260	
            },
            defaultType:'textfield',
            border:false,
            items:[{
                fieldLabel:'账号',
                blankText : '账号不能为空,请输入!',
                allowBlank:false,
                maxLength : 20,
                maxLengthText : '账号的最大长度为20个字符',	
                name:'account'
            },{
                xtype:'hidden', 
                name:'userId',
                value:'-1',
        	    anchor:'90%'
            },new Ext.ux.ComboBoxTree({																		
				fieldLabel : '机构',																			
				hiddenName : 'organName',																		
				allowBlank : false,																			
				blankText : '机构不能为空,请输入!',																
				tree : new Ext.tree.TreePanel({																
					rootVisible:false,																			
					loader: new Ext.tree.TreeLoader({														
						dataUrl:'organ/getOrganTree.jhtml' ,														
						clearOnLoad: true,																	
						listeners: {																		
							'beforeload': function(treeloader, node) {										
								treeloader.baseParams = {													
									id: node.id,															
									method: 'POST'															
								};																			
							}																				
						}																					
					}),																						
				 	root : new Ext.tree.AsyncTreeNode({id:'0',text:'根结点'})									
				 }), 																						
				 selectNodeModel:'leaf', 																	
				 listeners:{'afterchange':function(lnode,n,o){Ext.getCmp('uo_id').setValue(n.id);}}
			}),{																							
					fieldLabel : '密码',																			
					maxLength : 32,																				
					allowBlank : false,																			
					blankText : '密码不能为空,请输入!',																
					inputType : 'password',																		
					maxLengthText : '密码描述的最大长度为32个字符',														
					name : 'password'																			
				},{																								
					fieldLabel : '姓名',																			
					allowBlank : false,																			
					blankText : '姓名不能为空,请输入!',																
					maxLength : 30,																				
					maxLengthText : '姓名描述的最大长度为30个字符',														
					name : 'realName'																			
				},{																								
					xtype : 'combo',																			
					fieldLabel : '性别',																			
					hiddenName : 'sex',																			
					allowBlank : false,																			
					blankText : '性别不能为空,请输入!',																
					triggerAction: 'all',																		
					editable : false,																			
					store: Ext.data.sex																			
				},{																								
					fieldLabel : 'email',																		
					maxLength : 30,																				
					maxLengthText : 'email的最大长度为30个字符',														
					name : 'email'																				
				},{																								
					fieldLabel : '电话',																			
					maxLength : 30,																				
					maxLengthText : '电话的最大长度为30个字符',															
					name : 'mobile'																				
				},{																								
					fieldLabel : '备注',																			
					maxLength : 50,																				
					 xtype: 'textarea',																			
					maxLengthText : '备注的最大长度为50个字符',															
					name : 'remark'																				
				},{																								
					hidden : true,																				
					id : 'uo_id',																
					name : 'organId'																			
				}],																								
			buttons : [{																						
				text : '保存',																					
				iconCls : 'save',																				
				handler : user.save
			},{																									
				text : '重置',																					
				iconCls : 'refresh',																				
				handler :user.reset																						
			}]	
        };
	}
});

//保存数据
user.save = function(){
	var win = this.ownerCt.ownerCt.ownerCt;//当前win
	var form = this.ownerCt.ownerCt.form;																						
  	var userId = this.ownerCt.ownerCt.findByType('hidden')[0].getValue();								
	var url = userId==-1?'user/addUser.jhtml':'user/modUser.jhtml';												
	if (form.isValid()) {																					
		form.submit({																						
			url : url,																						
			waitTitle : '提示',																				
			method : 'POST',																				
			waitMsg : '正在提交数据,请稍候.....',																	
			success : function(form, action) {																
				Ext.Msg.alert('提示',action.result.msg, function() {											
					win.hide();
					Ext.getCmp(user.parentId).items.get(0).items.get(1).store.reload();//刷新grid
				});																							
			},																								
			failure : function(form, action) {																
				Ext.Msg.alert('提示',action.result.msg);																							
			}																								
		});																									
	}			
};

//清空表单
user.reset = function(){
	var win_form = this.ownerCt.ownerCt;
	win_form.form.reset();
};

user.roleWin = user.roleWin || Ext.extend(Ext.Window,{
	initComponent:function(){
		Ext.apply(this, {
			resizable : false,																					
			animateTarget : document.body,																		
			closeAction: 'hide',																				
			plain: true,
			modal: true,
			width: 230,
			height: 450,
			items:this.getTree(),
			buttons : [{
				text : '保存',																					
				iconCls : 'save',																				
				handler : user.saveRole2User
			},{																									
				text : '关闭',																					
				iconCls : 'close',																				
				handler : user.close
			}]
		}),
		user.roleWin.superclass.initComponent.call(this);
	},
	getTree:function(){
		return {
			xtype:'treepanel',
			height: 448,
			root : new Ext.tree.AsyncTreeNode({
				id : '0',
				text :'根节点',
				expanded : true
			}),
			rootVisible:false,
			loader : new Ext.tree.TreeLoader({
				baseAttrs : {},
				dataUrl : 'role/getRoleCheckBoxTree.jhtml',
				listeners: {
					'beforeload':function(treeloader, node) {
						treeloader.baseParams = {
							id: node.id,
							userId:Ext.getCmp(user.parentId).items.get(0).items.get(1).getSelectionModel().getSelected().data.userId,
							method: 'POST'
						};
					}
				}
			}),
			autoScroll : true
	  };
	}
});


user.close = function(){//关闭窗体
	this.ownerCt.ownerCt.hide();
};

user.saveRole2User=function(){
	var record = Ext.getCmp(user.parentId).items.get(0).items.get(1).getSelectionModel().getSelected();
	var data = this.ownerCt.ownerCt.items.get(0).getChecked('id')+'';
	var win = this.ownerCt.ownerCt;
	Ext.Ajax.request({
    	url:'user/saveRole2User.jhtml',
    	method:'POST',
    	params:{
    		userId:record.data.userId,
    		roleIds:data
    	},
    	success: function(resp,opts) {
           var respText = Ext.util.JSON.decode(resp.responseText); 
           Ext.Msg.alert('提示', respText.msg,function() {											
					win.hide();
					Ext.getCmp(user.parentId).items.get(0).items.get(1).store.reload();//刷新grid
			});
    	},
    	failure: function(resp,opts) {   
           var respText = Ext.util.JSON.decode(resp.responseText);  
            if(respText.msg !=''){
           		Ext.Msg.alert('错误',respText.msg);   
           }  
        }     
    });
};